package cn.quevo.cube.provide.data.tools;

/**
 * * @author July jul日期格式转换y_sky@foxmail.com
 *
 * @author July jul日期格式转换y_sky@foxmail.com
 * @version 1.0
 * @date 2018/12/14 6:58 PM
 * @Copyright 湘豫（北京）科技有限公司版权所有.All Rights Reserved.
 */
public class DateConvertFormatTools {

    /**
     * 日期格式转换
     *
     * @param date
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018/12/14 5:43 PM
     * @Copyright 湘豫（北京）科技有限公司版权所有.All Rights Reserved.
     */
    public static String convertDate(String date) {
        if (date.length() == 8) {
            return date.substring(0, 4).concat("/").concat(date.substring(4, 6).concat("/").concat(date.substring(6)));
        }
        return format(date.replaceAll(":\\.|\\-", "/"), "/");
    }

    /**
     * 时间格式转换
     *
     * @param time
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018/12/14 5:43 PM
     * @Copyright 湘豫（北京）科技有限公司版权所有.All Rights Reserved.
     */
    public static String convertTime(String time) {
        return format(time.replaceAll(":|\\.| |：", ":"), ":");
    }

    private static String format(String formatStr, String split) {
        String[] args = formatStr.split(split);
        StringBuilder timeBuilder = new StringBuilder();
        for (String str : args) {
            if (str.length() == 1) {
                timeBuilder.append("0");
            }
            timeBuilder.append(str).append(split);
        }
        return timeBuilder.substring(0, timeBuilder.length() - split.length());
    }

}
